﻿using Pay.Data;
using Pay.DTO;
using Pay.IService;
using Pay.Service.Cache;
using Pay.TO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Pay.Service {
    public class MerchIntroduceService : ServiceBase, IMerchIntroduceService, IDisposable {
        private IUnitOfWork _iUnitOfWork;
        private ISoupTaskService _iSoupTaskService;
        public MerchIntroduceService(IUnitOfWork iUnitOfWork, ISoupTaskService iSoupTaskService) : base(iUnitOfWork) {
            _iUnitOfWork = iUnitOfWork;
            _iSoupTaskService = iSoupTaskService;
        }

        /// <summary>
        /// 获得商家介绍列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public PagedResult<merch_introduce_dto> GetMerchIntroduces(MerchIntroduceQuery query) {
            var filter = PredicateBuilder.True<MerchIntroduce>();
            if (!string.IsNullOrEmpty(query.merch_name))
                filter = filter.And(c => c.MerchName == query.merch_name);
            if (!string.IsNullOrEmpty(query.status)) {
                int value = int.Parse(query.status);
                filter = filter.And(c => c.Status == (EMerchIntroduceStatus)value);
            }
            if (!string.IsNullOrEmpty(query.begin_time)) {
                DateTime time = DateTime.Parse(query.begin_time);
                filter = filter.And(c => c.CreateTime >= time);
            }
            if (!string.IsNullOrEmpty(query.end_time)) {
                DateTime time = DateTime.Parse(query.end_time).AddDays(1);
                filter = filter.And(c => c.CreateTime <= time);
            }
            var result = context.MerchIntroduce.FromCachePaged<MerchIntroduce, merch_introduce_dto>(query.page, query.size, filter, orderBy => orderBy.OrderBy(o => o.Status).ThenByDescending(o => o.IsRecommend));
            return result;
        }

        /// <summary>
        /// 获得商家介绍
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public merch_introduce_dto GetMerchIntroduce(long id) {
            return context.MerchIntroduce.Find(id).ToDtoObject<merch_introduce_dto>();
        }

        /// <summary>
        /// 新增商家介绍
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public int AddMerchIntroduce(merch_introduce_dto dto) {
            dto.create_time = DateTime.Now;
            MerchIntroduce model = new MerchIntroduce();
            dto.ToDtoObject(model);
            context.MerchIntroduce.Add(model);
            int result = _iUnitOfWork.Commit("MerchIntroduce");
            return result;
        }

        /// <summary>
        /// 更新商家介绍
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public int UpdateMerchIntroduce(merch_introduce_dto dto) {
            MerchIntroduce model = context.MerchIntroduce.FindById(dto.id);
            dto.ToDtoObject(model);
            int result = _iUnitOfWork.Commit("MerchIntroduce");
            return result;
        }

        /// <summary>
        /// 删除商家介绍
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int DeleteMerchIntroduce(long id) {
            var merchIntroduce = context.MerchIntroduce.FindById(id);
            context.MerchIntroduce.Remove(merchIntroduce);
            int result = _iUnitOfWork.Commit("MerchIntroduce");
            return result;
        }

        /// <summary>
        /// 修改商家状态
        /// </summary>
        /// <param name="id"></param>
        /// <param name="status"></param>
        /// <returns></returns>
        public int UpdateMerchIntroduceStatus(long id, int status) {
            MerchIntroduce model = context.MerchIntroduce.FindById(id);
            model.Status = (EMerchIntroduceStatus)(status);
            int result = _iUnitOfWork.Commit("MerchIntroduce");
            return result;
        }

        /// <summary>
        /// 修改商家是否推荐
        /// </summary>
        /// <param name="id"></param>
        /// <param name="isRecommend"></param>
        /// <returns></returns>
        public int UpdateMerchIntroduceIsRecommend(long id, bool isRecommend) {
            MerchIntroduce model = context.MerchIntroduce.FindById(id);
            model.IsRecommend = isRecommend;
            int result = _iUnitOfWork.Commit("MerchIntroduce");
            return result;
        }


        #region API接口
        /// <summary>
        /// 获得APP首页商家介绍列表
        /// </summary>
        /// <returns></returns>
        public List<merch_dto> GetAppIndexMerchIntroduceList() {
            var results = context.MerchIntroduce.Where(o => o.Status == EMerchIntroduceStatus.Normal && o.IsRecommend == true).OrderByDescending(o => o.CreateTime).FromCache<MerchIntroduce, merch_dto>("MerchIntroduce");
            return results;
        }
        /// <summary>
        /// 获得APP商家介绍列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public PagedResult<merch_dto> GetAppMerchIntroducePageList(PageQuery query) {
            var filter = PredicateBuilder.True<MerchIntroduce>();
            filter = filter.And(o => o.Status == EMerchIntroduceStatus.Normal);
            var result = context.MerchIntroduce.FromCachePaged<MerchIntroduce, merch_dto>(query.page, query.size, filter, orderBy => orderBy.OrderByDescending(c => c.CreateTime), "MerchIntroduce");
            return result;
        }
        /// <summary>
        /// App查看商家介绍详情
        /// </summary>
        /// <param name="id"></param>
        /// <param name="userId"></param>
        /// <returns></returns>
        public merch_detail_dto AppGetMerchIntroduce(long id, long userId) {
            //var result = context.MerchIntroduce.FindById(id).ToDtoObject<merch_detail_dto>();
            //SoupTask soupTask = context.SoupTask.Where(o => o.Type == ETaskType.MerchIntroduce).FirstOrDefault();
            //if (!context.UserSoupValueHis.Exist(o => o.UserId == userId && o.TaskType == ETaskType.MerchIntroduce && o.ItemId == id) && result.soup_value > 0) {//用户未领取，该篇商家介绍的马力值
            //    if (soupTask != null && (soupTask.LimitNum == null || soupTask.LimitNum > context.UserSoupValueHis.Where(o => o.UserId == userId && o.TaskType == ETaskType.MerchIntroduce).Count())) {//领取总次数不限制，或未达到领取次数限制
            //        result.isAllowGetSoupValue = true;
            //    }

            //}
            ////点击次数加1
            //var model = context.MerchIntroduce.FindById(id);
            //model.ClickNum = model.ClickNum + 1;
            ////任务点击数+1
            //if (soupTask != null) {
            //    soupTask.ClickNum = soupTask.ClickNum + 1;
            //}

            //_iUnitOfWork.Commit();
            //return result;
            return null;
        }

        #endregion
    }
}
